package cn.myschool.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebFilter("/*")
public class RoleFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入了RoleFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String path = request.getServletPath();
            if (path.equals("/denglu.jsp") ||path.endsWith(".do")|| path.equals("/dolenglu.jsp") || path.endsWith(".css") || path.endsWith(".js") || path.endsWith(".png")) {

            filterChain.doFilter(request, servletResponse);
            return;
        }
        Object user = request.getSession().getAttribute("USER_LOGIN");
        if (user == null) {
            request.setAttribute("msg", "您还没登录，不能访问" + request.getServletPath());
            request.getRequestDispatcher("/denglu.jsp").forward(request, servletResponse);
            return;
        }
        filterChain.doFilter(request, servletResponse);
        System.out.println("离开了RoleFilter");
    }

    @Override
    public void destroy() {

    }
}
